function varargout = ParseGUI(varargin)
% PARSEGUI M-file for ParseGUI.fig
%      PARSEGUI, by itself, creates a new PARSEGUI or raises the existing
%      singleton*.
%
%      H = PARSEGUI returns the handle to a new PARSEGUI or the handle to
%      the existing singleton*.
%
%      PARSEGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PARSEGUI.M with the given input arguments.
%
%      PARSEGUI('Property','Value',...) creates a new PARSEGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ParseGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ParseGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ParseGUI

% Last Modified by GUIDE v2.5 07-Feb-2008 19:22:26

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ParseGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @ParseGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

    

% --- Executes just before ParseGUI is made visible.
function ParseGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ParseGUI (see VARARGIN)

% Choose default command line output for ParseGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);


    
    handles.ParseData.M0imageArray = varargin{1};
	handles.ParseData.FreqimageArray = varargin{2};
    handles.ParseData.R2imageArray = varargin{3};
    handles.ParseData.CostimageArray = varargin{4};
    handles.ParseData.IterationValue = varargin{5};
%     handles.ParseData.TimeForFrame = varargin{6};
    
    axes(handles.M0map);    
    imagesc(handles.ParseData.M0imageArray); axis image;colormap gray;
    title(strcat('Estimated M0, ',num2str(handles.ParseData.IterationValue),' iterations'));
    
    axes(handles.Freqmap);
    imagesc(abs(handles.ParseData.FreqimageArray)); axis image;colormap gray;
    title(strcat('Estimated Freq, ',num2str(handles.ParseData.IterationValue),' iterations'));
    
    axes(handles.R2map);
    imagesc(handles.ParseData.R2imageArray); axis image;colormap gray;
    title(strcat('Estimated R2*, ',num2str(handles.ParseData.IterationValue),' iterations'));
    
    axes(handles.CostFunction);
    semilogy(handles.ParseData.CostimageArray); grid on;
    title(strcat('CC -  ',num2str(handles.ParseData.IterationValue),' iterations'));
    
%     axes(handles.TimeFrame);
%     bar(handles.ParseData.TimeForFrame); grid on;



guidata(hObject,handles);


% UIWAIT makes ParseGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = ParseGUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function lin_const_Callback(hObject, eventdata, handles)
handles.QuadData.lin_const = str2double(get(hObject,'String'));
guidata(hObject,handles)


function lin_const_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function sq_term_Callback(hObject, eventdata, handles)
handles.QuadData.sq_term = str2double(get(hObject,'String'));
guidata(hObject,handles)


function sq_term_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function x_sq_Callback(hObject, eventdata, handles)
handles.QuadData.x_sq = str2double(get(hObject,'String'));
guidata(hObject,handles)


function x_sq_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function y_sq_Callback(hObject, eventdata, handles)
handles.QuadData.y_sq = str2double(get(hObject,'String'));
guidata(hObject,handles)


function y_sq_CreateFcn(hObject, eventdata, handles)

if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function x_term_Callback(hObject, eventdata, handles)
handles.QuadData.x_term = str2double(get(hObject,'String'));
guidata(hObject,handles)


function x_term_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function y_term_Callback(hObject, eventdata, handles)
handles.QuadData.y_term = str2double(get(hObject,'String'));
guidata(hObject,handles)


function y_term_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function nlist_Callback(hObject, eventdata, handles)
handles.ParseData.nlist = str2num(get(hObject,'String'));
guidata(hObject,handles)


function nlist_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function offr_Callback(hObject, eventdata, handles)
handles.ParseData.offr = str2double(get(hObject,'String'));
guidata(hObject,handles)


function offr_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function ffac_Callback(hObject, eventdata, handles)
handles.ParseData.ffac = str2double(get(hObject,'String'));
guidata(hObject,handles)


function ffac_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function frnostart_Callback(hObject, eventdata, handles)
handles.ParseData.frnostart = str2double(get(hObject,'String'));
guidata(hObject,handles)



function frnostart_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function wfr_Callback(hObject, eventdata, handles)
handles.ParseData.wfr = str2double(get(hObject,'String'));
guidata(hObject,handles)


function wfr_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function wfi_Callback(hObject, eventdata, handles)
handles.ParseData.wfi = str2double(get(hObject,'String'));
guidata(hObject,handles)


function wfi_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function frnoend_Callback(hObject, eventdata, handles)
handles.ParseData.frnoend = str2double(get(hObject,'String'));
guidata(hObject,handles)


function frnoend_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function fidfile_Callback(hObject, eventdata, handles)
handles.ParseData.fidfile = str2double(get(hObject,'String'));
guidata(hObject,handles)


function fidfile_CreateFcn(hObject, eventdata, handles)
if isunix && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in browseFID.
function browseFID_Callback(hObject, eventdata, handles)
% hObject    handle to browseFID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.ParseData.fidDirectory = uigetdir('/media/sda1/Parse/data/phantom_studies'); % Set this to directory of your choice
handles.ParseData.StudyDir = strcat(handles.ParseData.fidDirectory(findstr(handles.ParseData.fidDirectory,'pine'):end));
handles.ParseData.Study = handles.ParseData.fidDirectory(findstr(handles.ParseData.fidDirectory,'s_20'):findstr(handles.ParseData.fidDirectory,'/data/pi')-1);
set(handles.fidfile, 'String', strcat(handles.ParseData.fidDirectory));

guidata(hObject,handles)



% --- Executes on button press in ParamVerify.
function ParamVerify_Callback(hObject, eventdata, handles)
% hObject    handle to ParamVerify (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% if (exist('handles.ParseData.gmax')==0) && (exist('handles.ParseData.thk')==0)
%     handles.ParseData.gmax = 1.9;
%     handles.ParseData.thk = 2;
%     errordlg('Gmax and Thickness not set. Defaulting gmax = 1.9; thickness = 2 mm ','Error');
% end

clc;
set(handles.gmaxverify, 'String', strcat('Gmax',char([32 58]), num2str(handles.ParseData.gmax)));
set(handles.ffacVerify, 'String', strcat('ffac',char([32 58]), num2str(handles.ParseData.ffac)));
set(handles.offrVerify, 'String', strcat('offr',char([32 58]), num2str(handles.ParseData.offr)));
set(handles.thkVerify, 'String', strcat('Thickness', char([32 58]), num2str(handles.ParseData.thk),' mm'));
set(handles.wfrwfiVerify, 'String', strcat('wfr :',num2str(handles.ParseData.wfr), ' wfi :',num2str(handles.ParseData.wfi)));
set(handles.ipfile, 'String', strcat('FID file :', handles.ParseData.StudyDir));
set(handles.studyVerify, 'String', strcat('Study :', handles.ParseData.Study));
set(handles.startxVerify, 'String', strcat('startx :', num2str(handles.ParseData.startx)));
set(handles.endxVerify, 'String', strcat('endx :', num2str(handles.ParseData.endx)));
set(handles.n1Verify, 'String', strcat('N1 :', num2str(handles.ParseData.N1)));
set(handles.TrajFile, 'String', strcat('Traj :', handles.ParseData.trajectory_file));
set(handles.FreqEquation, 'String', strcat(num2str(handles.QuadData.lin_const), ' + ', num2str(handles.QuadData.sq_term), '((',...
    num2str(handles.QuadData.x_sq), '.x^2) + (', num2str(handles.QuadData.y_sq), '.y^2)) + (', num2str(handles.QuadData.x_term),...
    '.x) + (', num2str(handles.QuadData.y_term), '.y)'));
set(handles.framesVerify, 'String', strcat('Frames : ',num2str(handles.ParseData.frnostart),...
    '-',num2str(handles.ParseData.frnoend)));
%Output .mat file
handles.ParseData.OutputMatFile = strcat('p_',handles.ParseData.Study,'gmax_',num2str(handles.ParseData.gmax),...
    '_thk',num2str(handles.ParseData.thk),'.mat');
set(handles.opmatfile, 'String', strcat('o/p :', handles.ParseData.OutputMatFile));
%Output location
handles.ParseData.OutputDirectory = strcat('/media/sda1/Parse/results/', handles.ParseData.Study, '/parse');
set(handles.opdirectory, 'String', strcat('Dir :', handles.ParseData.OutputDirectory));

guidata(hObject,handles)



% --- Executes on button press in logParameters.
function logParameters_Callback(hObject, eventdata, handles)
% hObject    handle to logParameters (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
current_study = handles.ParseData.Study;
current_thickness = num2str(handles.ParseData.thk);
current_gmax = num2str(handles.ParseData.gmax);
current_offr = num2str(handles.ParseData.offr);
current_ffac = num2str(handles.ParseData.ffac);
current_wfr = num2str(handles.ParseData.wfr);
current_wfi = num2str(handles.ParseData.wfi);
current_lin_const = num2str(handles.QuadData.lin_const);
current_sq_term = num2str(handles.QuadData.sq_term);
current_x_sq = num2str(handles.QuadData.x_sq);
current_y_sq = num2str(handles.QuadData.y_sq);
current_x_term = num2str(handles.QuadData.x_term);
current_y_term = num2str(handles.QuadData.y_term);
current_nlist = num2str(handles.ParseData.nlist);
Data_Separator = '===============================================';

LogFileNAME = strcat(current_study,'_gmax',num2str(current_gmax),'_thk',num2str(current_thickness),'.log');
%handles.ParseData.OutputMatFile = strcat(LogFileNAME(1:findstr(LogFileNAME,'.log')-1),'.mat');

fileIOvar = fopen(LogFileNAME, 'a+');

if handles.ParseData.GoodParameters == 1
    fprintf(fileIOvar, ['\n*These Parameters Give Good Estimation*\n\n']);
end

fprintf(fileIOvar, ['Time: ' datestr(now) '\n\nOff Resonance (offr): ' current_offr '\nFID Scaling(ffac): ' current_ffac '\nwfr:'...
    current_wfr ';  wfi: ' current_wfi '\n\nQuadratic Terms:\nX^2: ' current_x_sq '\nY^2: ' current_y_sq  '\nX: ' current_x_term ...
    '\nY: ' current_y_term '\nConstant: ' current_lin_const '\n\nNLIST: ' current_nlist '\n\n' Data_Separator '\n\n']);

fclose('all');

guidata(hObject,handles)

% --- Executes on button press in resetParams.
function resetParams_Callback(hObject, eventdata, handles)
% hObject    handle to resetParams (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc;
clc
handles.ParseData.gmax=1.9;     %set(handles.GmaxUndThk, 'SelectedObject', handles.gmax19);
handles.ParseData.thk=2;        %set(handles.GmaxUndThk, 'SelectedObject', handles.thk2);
handles.ParseData.GoodParameters = 0;
handles.ParseData.ffac=1;       set(handles.ffac, 'String', num2str(handles.ParseData.ffac));
handles.ParseData.offr=0;       set(handles.offr, 'String', num2str(handles.ParseData.offr));
handles.ParseData.frnostart=1;  set(handles.frnostart, 'String', num2str(handles.ParseData.frnostart));
handles.ParseData.frnoend=50;   set(handles.frnoend, 'String', num2str(handles.ParseData.frnoend));
handles.ParseData.wfr=1;        set(handles.wfr, 'String', num2str(handles.ParseData.wfr));
handles.ParseData.wfi=1;        set(handles.wfi, 'String', num2str(handles.ParseData.wfi));
handles.QuadData.lin_const=0;   set(handles.lin_const, 'String', num2str(handles.QuadData.lin_const));
handles.QuadData.sq_term=0;     set(handles.sq_term, 'String', num2str(handles.QuadData.sq_term));
handles.QuadData.x_sq=0;        set(handles.x_sq, 'String', num2str(handles.QuadData.x_sq));
handles.QuadData.y_sq=0;        set(handles.y_sq, 'String', num2str(handles.QuadData.y_sq));
handles.QuadData.x_term=0;      set(handles.x_term, 'String', num2str(handles.QuadData.x_term));
handles.QuadData.y_term=0;      set(handles.y_term, 'String', num2str(handles.QuadData.y_term));
load trajvar;
arrayvar=findstr(ismember(trajvar.gmax,handles.ParseData.gmax)',1);
handles.ParseData.startx=trajvar.startx(arrayvar);
handles.ParseData.gmax=trajvar.gmax(arrayvar);
handles.ParseData.endx=trajvar.endx(arrayvar);
handles.ParseData.np=trajvar.np(arrayvar);
handles.ParseData.at=trajvar.at(arrayvar);
handles.ParseData.sw=trajvar.sw(arrayvar);
handles.ParseData.N1=trajvar.N1(arrayvar);
handles.ParseData.trajectory_file=strcat(trajvar.ktraj(arrayvar,:));
handles.ParseData.StudyDir='None Selected';
handles.ParseData.Study='None Selected';
set(handles.fidfile, 'String', 'Please select a FID directory');

guidata(hObject,handles)



% --- Executes on button press in run.
function run_Callback(hObject, eventdata, handles)
% hObject    handle to run (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

offr = handles.ParseData.offr;
ffac = handles.ParseData.ffac;
gmax = handles.ParseData.gmax;
thk = handles.ParseData.thk;
startx = handles.ParseData.startx;
endx = handles.ParseData.endx;
np = handles.ParseData.np;
Ta = handles.ParseData.at;
sw = handles.ParseData.sw;
N1 = 3*handles.ParseData.N1;
trajectory_file=handles.ParseData.trajectory_file;
fid_file = handles.ParseData.fidDirectory;
fid_file=fid_file(1:findstr(fid_file,'.fid')-1);
wfr = handles.ParseData.wfr;
wfi = handles.ParseData.wfi;
NLIST = [handles.ParseData.nlist].'*N1;
frnostart = handles.ParseData.frnostart;
frnoend = handles.ParseData.frnoend;
OutputDirectory = handles.ParseData.OutputDirectory;
OutputMatFileName = handles.ParseData.OutputMatFile;

lin_const = handles.QuadData.lin_const;
sq_term = handles.QuadData.sq_term;
x_sq = handles.QuadData.x_sq;
y_sq = handles.QuadData.y_sq;
x_term = handles.QuadData.x_term;
y_term = handles.QuadData.y_term;
StudyName = handles.ParseData.Study;

save estimationfile N1 thk endx gmax np trajectory_file NLIST startx wfi Ta...
    fid_file sw wfr frnostart frnoend OutputDirectory OutputMatFileName offr...
    lin_const sq_term x_sq y_sq x_term y_term StudyName
autoparse;


% --- Executes on button press in accept.
function accept_Callback(hObject, eventdata, handles)
% hObject    handle to accept (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in gmax19.
function gmax19_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
    handles.gmax=1.9;
    load trajvar;
    clear handles.ParseData.startx handles.ParseData.gmax handles.ParseData.endx...
        handles.ParseData.np handles.ParseData.at handles.ParseData.sw...
        handles.ParseData.trajectory_file handles.ParseData.N1
    arrayvar=findstr(ismember(trajvar.gmax,handles.gmax)',1);
    handles.ParseData.startx=trajvar.startx(arrayvar);
    handles.ParseData.gmax=trajvar.gmax(arrayvar);
    handles.ParseData.endx=trajvar.endx(arrayvar);
    handles.ParseData.np=trajvar.np(arrayvar);
    handles.ParseData.at=trajvar.at(arrayvar);
    handles.ParseData.sw=trajvar.sw(arrayvar);
    handles.ParseData.N1=trajvar.N1(arrayvar);
    handles.ParseData.trajectory_file=strcat(trajvar.ktraj(arrayvar,:));
    set(handles.gmaxverify, 'String', strcat('Gmax',char([32 58]), num2str(handles.ParseData.gmax)));
    set(handles.TrajFile, 'String', strcat('Traj', char([32 58]), handles.ParseData.trajectory_file));
end
guidata(hObject,handles)


% --- Executes on button press in gmax23.
function gmax23_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
    handles.gmax=2.3;
    load trajvar;
    clear handles.ParseData.startx handles.ParseData.gmax handles.ParseData.endx...
        handles.ParseData.np handles.ParseData.at handles.ParseData.sw...
        handles.ParseData.trajectory_file handles.ParseData.N1
    arrayvar=findstr(ismember(trajvar.gmax,handles.gmax)',1);
    handles.ParseData.startx=trajvar.startx(arrayvar);
    handles.ParseData.gmax=trajvar.gmax(arrayvar);
    handles.ParseData.endx=trajvar.endx(arrayvar);
    handles.ParseData.np=trajvar.np(arrayvar);
    handles.ParseData.at=trajvar.at(arrayvar);
    handles.ParseData.sw=trajvar.sw(arrayvar);
    handles.ParseData.N1=trajvar.N1(arrayvar);
    handles.ParseData.trajectory_file=strcat(trajvar.ktraj(arrayvar,:));
    set(handles.gmaxverify, 'String', strcat('Gmax',char([32 58]), num2str(handles.ParseData.gmax)));
    set(handles.TrajFile, 'String', strcat('Traj', char([32 58]), handles.ParseData.trajectory_file));
end
guidata(hObject,handles)


% --- Executes on button press in gmax25.
function gmax25_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
    handles.gmax=2.5;
    load trajvar;
    clear handles.ParseData.startx handles.ParseData.gmax handles.ParseData.endx...
        handles.ParseData.np handles.ParseData.at handles.ParseData.sw...
        handles.ParseData.trajectory_file handles.ParseData.N1
    arrayvar=findstr(ismember(trajvar.gmax,handles.gmax)',1);
    handles.ParseData.startx=trajvar.startx(arrayvar);
    handles.ParseData.gmax=trajvar.gmax(arrayvar);
    handles.ParseData.endx=trajvar.endx(arrayvar);
    handles.ParseData.np=trajvar.np(arrayvar);
    handles.ParseData.at=trajvar.at(arrayvar);
    handles.ParseData.sw=trajvar.sw(arrayvar);
    handles.ParseData.N1=trajvar.N1(arrayvar);
    handles.ParseData.trajectory_file=strcat(trajvar.ktraj(arrayvar,:));
    set(handles.gmaxverify, 'String', strcat('Gmax',char([32 58]), num2str(handles.ParseData.gmax)));
    set(handles.TrajFile, 'String', strcat('Traj', char([32 58]), handles.ParseData.trajectory_file));
end
guidata(hObject,handles)


% --- Executes on button press in gmax29.
function gmax29_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
    handles.gmax=2.9;
    load trajvar;
    clear handles.ParseData.startx handles.ParseData.gmax handles.ParseData.endx...
        handles.ParseData.np handles.ParseData.at handles.ParseData.sw...
        handles.ParseData.trajectory_file handles.ParseData.N1
    arrayvar=findstr(ismember(trajvar.gmax,handles.gmax)',1);
    handles.ParseData.startx=trajvar.startx(arrayvar);
    handles.ParseData.gmax=trajvar.gmax(arrayvar);
    handles.ParseData.endx=trajvar.endx(arrayvar);
    handles.ParseData.np=trajvar.np(arrayvar);
    handles.ParseData.at=trajvar.at(arrayvar);
    handles.ParseData.sw=trajvar.sw(arrayvar);
    handles.ParseData.N1=trajvar.N1(arrayvar);
    handles.ParseData.trajectory_file=strcat(trajvar.ktraj(arrayvar,:));
    set(handles.gmaxverify, 'String', strcat('Gmax',char([32 58]), num2str(handles.ParseData.gmax)));
    set(handles.TrajFile, 'String', strcat('Traj', char([32 58]), handles.ParseData.trajectory_file));
end
guidata(hObject,handles)


% --- Executes on button press in gmax32.
function gmax32_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
    handles.gmax=3.2;
    load trajvar;
    clear handles.ParseData.startx handles.ParseData.gmax handles.ParseData.endx...
        handles.ParseData.np handles.ParseData.at handles.ParseData.sw...
        handles.ParseData.trajectory_file handles.ParseData.N1
    arrayvar=findstr(ismember(trajvar.gmax,handles.gmax)',1);
    handles.ParseData.startx=trajvar.startx(arrayvar);
    handles.ParseData.gmax=trajvar.gmax(arrayvar);
    handles.ParseData.endx=trajvar.endx(arrayvar);
    handles.ParseData.np=trajvar.np(arrayvar);
    handles.ParseData.at=trajvar.at(arrayvar);
    handles.ParseData.sw=trajvar.sw(arrayvar);
    handles.ParseData.N1=trajvar.N1(arrayvar);
    handles.ParseData.trajectory_file=strcat(trajvar.ktraj(arrayvar,:));
    set(handles.gmaxverify, 'String', strcat('Gmax',char([32 58]), num2str(handles.ParseData.gmax)));
    set(handles.TrajFile, 'String', strcat('Traj', char([32 58]), handles.ParseData.trajectory_file));
end
guidata(hObject,handles)


% --- Executes on button press in gmax35.
function gmax35_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
    handles.gmax=3.5;
    load trajvar;
    clear handles.ParseData.startx handles.ParseData.gmax handles.ParseData.endx...
        handles.ParseData.np handles.ParseData.at handles.ParseData.sw...
        handles.ParseData.trajectory_file handles.ParseData.N1
    arrayvar=findstr(ismember(trajvar.gmax,handles.gmax)',1);
    handles.ParseData.startx=trajvar.startx(arrayvar);
    handles.ParseData.gmax=trajvar.gmax(arrayvar);
    handles.ParseData.endx=trajvar.endx(arrayvar);
    handles.ParseData.np=trajvar.np(arrayvar);
    handles.ParseData.at=trajvar.at(arrayvar);
    handles.ParseData.sw=trajvar.sw(arrayvar);
    handles.ParseData.N1=trajvar.N1(arrayvar);
    handles.ParseData.trajectory_file=strcat(trajvar.ktraj(arrayvar,:));
    set(handles.gmaxverify, 'String', strcat('Gmax',char([32 58]), num2str(handles.ParseData.gmax)));
    set(handles.TrajFile, 'String', strcat('Traj', char([32 58]), handles.ParseData.trajectory_file));
end
guidata(hObject,handles)


% --- Executes on button press in gmax38.
function gmax38_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
    handles.gmax=3.8;
    load trajvar;
    clear handles.ParseData.startx handles.ParseData.gmax handles.ParseData.endx...
        handles.ParseData.np handles.ParseData.at handles.ParseData.sw...
        handles.ParseData.trajectory_file handles.ParseData.N1
    arrayvar=findstr(ismember(trajvar.gmax,handles.gmax)',1);
    handles.ParseData.startx=trajvar.startx(arrayvar);
    handles.ParseData.gmax=trajvar.gmax(arrayvar);
    handles.ParseData.endx=trajvar.endx(arrayvar);
    handles.ParseData.np=trajvar.np(arrayvar);
    handles.ParseData.at=trajvar.at(arrayvar);
    handles.ParseData.sw=trajvar.sw(arrayvar);
    handles.ParseData.N1=trajvar.N1(arrayvar);
    handles.ParseData.trajectory_file=strcat(trajvar.ktraj(arrayvar,:));
    set(handles.gmaxverify, 'String', strcat('Gmax',char([32 58]), num2str(handles.ParseData.gmax)));
    set(handles.TrajFile, 'String', strcat('Traj', char([32 58]), handles.ParseData.trajectory_file));
end
guidata(hObject,handles)


% --- Executes on button press in thk2.
function thk2_Callback(hObject, eventdata, handles)
% hObject    handle to thk2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of thk2
if get(hObject,'Value') ==1
    clear handles.ParseData.thk;
    handles.ParseData.thk = 2;
    set(handles.thkVerify, 'String', strcat('Thickness', char([32 58]), num2str(handles.ParseData.thk),' mm'));
end
guidata(hObject,handles)


% --- Executes on button press in thk3.
function thk3_Callback(hObject, eventdata, handles)

if get(hObject,'Value') ==1
    clear handles.ParseData.thk;
    handles.ParseData.thk = 3;
    set(handles.thkVerify, 'String', strcat('Thickness', char([32 58]), num2str(handles.ParseData.thk),' mm'));
end
guidata(hObject,handles)


% --- Executes on button press in thk4.
function thk4_Callback(hObject, eventdata, handles)

if get(hObject,'Value') ==1
    clear handles.ParseData.thk;
    handles.ParseData.thk = 4;
    set(handles.thkVerify, 'String', strcat('Thickness', char([32 58]), num2str(handles.ParseData.thk),' mm'));
end
guidata(hObject,handles)


% --- Executes on button press in thk5.
function thk5_Callback(hObject, eventdata, handles)

if get(hObject,'Value') ==1
    clear handles.ParseData.thk;
    handles.ParseData.thk = 5;
    set(handles.thkVerify, 'String', strcat('Thickness', char([32 58]), num2str(handles.ParseData.thk),' mm'));
end
guidata(hObject,handles)


% --- Executes on button press in ParamsVeryGood.
function ParamsVeryGood_Callback(hObject, eventdata, handles)
% hObject    handle to ParamsVeryGood (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of ParamsVeryGood
if get(hObject,'Value') == 1
    handles.ParseData.GoodParameters = 1;
else
    handles.ParseData.GoodParameters = 0;
end
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function M0map_CreateFcn(hObject, eventdata, handles)


% hObject    handle to M0map (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate M0map
           
